package com.ymatou.iapi.productcategory.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productcategory.parameter.BrandAliasDto;
import com.ymatou.iapi.productcategory.parameter.BrandCategoryDto;
import com.ymatou.iapi.productcategory.parameter.BrandDto;
import com.ymatou.iapi.productcategory.parameter.BrandInfo;
import com.ymatou.iapi.productcategory.parameter.BrandTagDto;
import com.ymatou.iapi.productcategory.parameter.CatalogsTagDto;
import com.ymatou.iapi.productcategory.parameter.CountryDto;
import com.ymatou.iapi.productcategory.parameter.GetBrandInfoByIdBean;
import com.ymatou.iapi.productcategory.parameter.SaveBrandBean;
import com.ymatou.iapi.productcategory.service.GetBrandInfoByIdCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.BuyerProductWapper;
import com.ymttest.database.sqlwapper.YmtProductBrandWapper;
import com.ymttest.utils.TestDataManager;
/**
*查询品牌详细信息（后台）
*FreeMaker Template自动生成代码
*/
/**
 * @author zhouyi
 *
 */
@TestSuite
public class Ts_GetBrandInfoById{
	private static GetBrandInfoByIdBean getbrandinfobyidBean;
	private static GetBrandInfoByIdCall getbrandinfobyidCall;
	static BuyerProductWapper pbinfow ;
	@BeforeClass
	public static void setUp() throws NumberFormatException, Exception {
		Logger.createResultFile("查询品牌详细信息（后台）");
		pbinfow= new BuyerProductWapper();
	}

	@Before
	public void caseUp() {
		getbrandinfobyidBean=new GetBrandInfoByIdBean();
		getbrandinfobyidCall=new GetBrandInfoByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbrandinfobyidCall.getOpurl());
		pbinfow.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBrandInfoById_001() {
		Logger.start(true,"查询品牌详细信息（后台）");
		try {
			
			ArrayList<Map> list = pbinfow.getProductBrandInfoById(21570);
			Map map = list.get(0);
			String BrandId = map.get("BrandId").toString();
			getbrandinfobyidBean.setBrandId(Integer.valueOf(BrandId));
			getbrandinfobyidCall.setData(getbrandinfobyidBean);
			getbrandinfobyidCall.callService();			
			Logger.verifyEquals("200", getbrandinfobyidCall.getString("Code"), "验证Code");
			BrandInfo Brand=(BrandInfo) FormatUtil.GSONToObject(getbrandinfobyidCall.getJsonObject().get("Data").getAsJsonObject().get("BrandInfo").toString(), BrandInfo.class);
			Logger.verifyEquals(map.get("BrandId"), Brand.getId(), "验证id");
			Logger.verifyEquals(map.get("KeyWord"), Brand.getKeyWord(), "验证KeyWord");
			Logger.verifyEquals(map.get("BrandEnName"), Brand.getEnName(), "验证BrandEnName");
			Logger.verifyEquals(map.get("CountryId"), Brand.getCountryId(), "验证CountryId");
			Logger.verifyEquals(map.get("BrandLogoUrl"), Brand.getLogoUrl(), "验证LogoUrl");
			Logger.verifyEquals(map.get("BrandBannerUrl"), Brand.getBannerUrl(), "验证BrandBannerUrl");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBrandInfoById_002() {
		Logger.start(true,"查询品牌详细信息（后台）-新增字段");
		try {
			SaveBrandBean SaveBrandBean = initSavebrand();
			Integer BrandId = SaveBrandCall(SaveBrandBean);
			getbrandinfobyidBean.setBrandId(BrandId);
			getbrandinfobyidCall.setData(getbrandinfobyidBean);
			getbrandinfobyidCall.callService();			
			Logger.verifyEquals("200", getbrandinfobyidCall.getString("Code"), "验证Code");
			BrandInfo Brand=(BrandInfo) FormatUtil.GSONToObject(getbrandinfobyidCall.getJsonObject().get("Data").getAsJsonObject().get("BrandInfo").toString(), BrandInfo.class);
			verifyData(SaveBrandBean, Brand);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * @param savebrandBean
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	private Integer SaveBrandCall(SaveBrandBean savebrandBean) throws ClientProtocolException, IOException {
		com.ymatou.iapi.productcategory.service.SaveBrandCall savebrandCall=new com.ymatou.iapi.productcategory.service.SaveBrandCall();
		savebrandCall.setData(savebrandBean);
		savebrandCall.callService();			
		Integer bid=savebrandCall.getBrandId();
		return bid;
	}
	/**
	 * @return
	 */
	public  SaveBrandBean initSavebrand() {
		SaveBrandBean savebrandBean=new SaveBrandBean();
		Integer abs = Math.abs(TestDataManager.getRandomIntNum(10));
		savebrandBean.setGeneralType(2);
		savebrandBean.setGeneralName("通用显示名大牌");
		savebrandBean.setShow(true);
		savebrandBean.setOtherTags("other,test");
		List<CatalogsTagDto> catalogsTagList=new ArrayList<CatalogsTagDto>();
		CatalogsTagDto c=new CatalogsTagDto();
		c.setTagId(1);
		c.setBrandTagValue("brandtagvalue");
		catalogsTagList.add(c);
		CatalogsTagDto c2=new CatalogsTagDto();
		c2.setTagId(2);
		c2.setBrandTagValue("brandtagvalue2");
		catalogsTagList.add(c2);
		
		savebrandBean.setBrandTagList(catalogsTagList);
		savebrandBean.setCnName("测试品牌005号");
		savebrandBean.setEnName(abs.toString());
		savebrandBean.setKeyWord("T");
		savebrandBean.setCountryId(004);
		savebrandBean.setDefaultName(1);
		savebrandBean.setType(2);
		BrandCategoryDto brandCategoryDto = new BrandCategoryDto();
		brandCategoryDto.setCategoryEnName("hhahhha");
		brandCategoryDto.setCategoryId(10086);
		brandCategoryDto.setCategoryName("书籍是人lei的阶梯");
		brandCategoryDto.setCategoryPath("书籍 -> 四大名著 -> 西游记");
		brandCategoryDto.setLevel(2);
		brandCategoryDto.setMasterCategoryId(10881);
		List<BrandCategoryDto> categoryList = new ArrayList<BrandCategoryDto>();
		categoryList.add(brandCategoryDto);
		
		savebrandBean.setCategoryList(categoryList);
		CountryDto countryDto = new CountryDto();
		countryDto.setContinentName("湖北省");
		countryDto.setCountryId(001);
		countryDto.setCountryName("中国");
		List<CountryDto> comnCountryList = new ArrayList<CountryDto>();
		comnCountryList.add(countryDto);
		
		savebrandBean.setComnCountryList(comnCountryList);
		savebrandBean.setDescription("这是一条测试数据");
		savebrandBean.setLogoUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533271935029&di=c20549998621dfd6ee14b41b92db97fa&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F018373556848c8000001271679582d.jpg");
		savebrandBean.setBannerUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533271995519&di=9f040e778034bf7381c75488e063d03f&imgtype=0&src=http%3A%2F%2Fwww.jituwang.com%2Fuploads%2Fallimg%2F140428%2F259428-14042PS62466.jpg");
		BrandAliasDto brandAliasDto = new BrandAliasDto();
		brandAliasDto.setId(0001);
		brandAliasDto.setName("玄幻之祖");
		List<BrandAliasDto> aliasList = new ArrayList<BrandAliasDto>();
		aliasList.add(brandAliasDto);
		
		savebrandBean.setAliasList(aliasList);
		savebrandBean.setFounder("孙悟空");
		savebrandBean.setUpdatUser("猪八戒");
		return savebrandBean;
	}
	private void verifyData(SaveBrandBean savebrandBean2, BrandInfo brandInfo) {
		Logger.verifyEquals(savebrandBean2.getCnName(), brandInfo.getCnName(), "验证sBrand");
		Logger.verifyEquals(savebrandBean2.getEnName(), brandInfo.getEnName(), "验证sBrandEn");
		Logger.verifyEquals(savebrandBean2.getDescription(), brandInfo.getDescription(), "验证mBrandDescript");
		Logger.verifyEquals(savebrandBean2.getFounder(), brandInfo.getFounder(), "验证sFounder");
		Logger.verifyEquals(savebrandBean2.getLogoUrl(), brandInfo.getLogoUrl(), "验证sLogoUrl");
		Logger.verifyEquals(savebrandBean2.getBannerUrl(), brandInfo.getBannerUrl(), "验证sBannerUrl");
		Logger.verifyEquals(savebrandBean2.getKeyWord(), brandInfo.getKeyWord(), "验证sKey");
		Logger.verifyEquals(savebrandBean2.getCountryId(), brandInfo.getCountryId(), "验证iCountryId");
		Logger.verifyEquals(savebrandBean2.getType(), brandInfo.getType(), "验证sBrandType");
		if(savebrandBean2.getShow()!=null) {
			Logger.verifyEquals(savebrandBean2.getShow(), brandInfo.getShow(), "验证show");
		}
		if(savebrandBean2.getGeneralName()!=null) {
			Logger.verifyEquals(savebrandBean2.getGeneralName(), brandInfo.getGeneralName(), "验证generalName");
		}
		if(savebrandBean2.getGeneralType()!=null) {
			Logger.verifyEquals(savebrandBean2.getGeneralType(), brandInfo.getGeneralType(), "验证generalType");
		}
		if(savebrandBean2.getOtherTags()!=null) {
			Logger.verifyEquals(savebrandBean2.getOtherTags(), brandInfo.getOtherTags(), "验证otherTags");
		}
		savebrandBean2.getBrandTagList().forEach(x->{
			BrandTagDto db1 = brandInfo.getBrandTagList().stream().filter(y-> y.getTagId().equals(x.getTagId())).findFirst().get();
			Logger.verifyEquals(db1.getBrandTagValue(), x.getBrandTagValue(), "验证value");
		});
		
	}
}